﻿using MAY.CHANEL.Models;
using MAY.CHANEL.ViewModel;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;

namespace MAY.CHANEL.Controllers
{
    public class StuController : Controller
    {
        // GET: Stu
        [HttpGet]
        public ActionResult Student()
        {
            return View();
        }

        [HttpPost]
        public ActionResult Student(StuSelViewModel StuView)
        {
            using (var db = new StudentModelEntity())
            {
                var space = db.Stu.AsQueryable();
                if (!string.IsNullOrWhiteSpace(StuView.CourseName))
                {
                    space = space.Where(s => s.StuName.Contains(StuView.CourseName.Trim()));
                }
                if (StuView.CourseType > 0)
                {
                    space = space.Where(s => s.ClassId == StuView.CourseType);
                }
                var list = space.Select(s=>new StudentViewModel()
                {
                    Id=s.Id,
                    ClassId=s.ClassId,
                    ClassName=s.Class.ClassName,
                    StuName=s.StuName,
                    Age=s.Age,
                     Sex=s.Sex
                }).OrderBy(s=>s.Id).Skip((StuView.Page-1)*StuView.Limit).Take(StuView.Limit).ToList();
                var totle = space.Count();
                return Json(new { code = 0, msg = "加载成功", count = totle, data = list });
            }
                
        }

        public ActionResult Index()
        {
            return View();
        }

        [HttpGet]
        public ActionResult StudentAdd()
        {
            return View();
        }

        [HttpPost]
        public ActionResult StudentAdd(StuAddViewModel stuAdd)
        {
            using (var db = new StudentModelEntity())
            {
                var model = new Stu()
                {
                    ClassId = stuAdd.ClassId,
                    StuName = stuAdd.StuName,
                    Age = stuAdd.Age,
                    Sex = stuAdd.Sex
                };
                db.Stu.Add(model);
                if (db.SaveChanges() > 0)
                {
                    return Json(new Operate(result.succes,"添加成功"));
                }
                else
                {
                    return Json(new Operate(result.erroe,"添加失败"));
                }
            }
        }

        [HttpGet]
        public ActionResult StudentEdit(int id)
        {
            using (var db = new StudentModelEntity())
            {
                var model = db.Stu.FirstOrDefault(s => s.Id == id);
                if (model != null)
                {
                    var viewModel = new StudentEditViewModel()
                    {
                        Id = model.Id,
                        ClassId = Convert.ToInt32(model.ClassId),
                        Age = model.Age,
                        Sex = model.Sex,
                        StuName = model.StuName
                    };
                    ViewData["List"] = Bind();
                    return View(viewModel);
                }
                else
                {
                    return HttpNotFound();
                }
            }
        }
        [HttpPost]
        public ActionResult StudentEdit(StudentEditViewModel viewModel)
        {
            using (var db = new StudentModelEntity())
            {
                var model = db.Stu.FirstOrDefault(s => s.Id == viewModel.Id);
                if (model != null)
                {
                    model.Sex = viewModel.Sex;
                    model.ClassId = viewModel.ClassId;
                    model.Age = viewModel.Age;
                    model.StuName = viewModel.StuName;
                    if (db.SaveChanges() > 0)
                    {
                        return Json(new Operate(result.succes, "编辑成功"));
                    }
                    else
                    {
                        return Json(new Operate(result.erroe, "编辑失败"));
                    }
                }
                else
                {
                    return Json(new Operate(result.erroe, "更新数据不存在"));
                }
            }
        }

        [HttpPost]
        public ActionResult StudentDel(int DelId)
        {
            using (var db = new StudentModelEntity())
            {
                var model = db.Stu.FirstOrDefault(s => s.Id == DelId);
                if (model != null)
                {
                    db.Stu.Remove(model);
                    if (db.SaveChanges() > 0)
                    {
                        return Json(new Operate(result.succes, "删除成功!"));
                    }
                    else
                    {
                        return Json(new Operate(result.erroe, "删除失败!"));
                    }
                }
                else
                {
                    return Json(new Operate(result.erroe, "数据不存在!"));
                }
            }
        }

        [HttpGet]
        public ActionResult Teacher()
        {
            return View();
        }

        [HttpPost]
        public ActionResult Teacher(StuSelViewModel StuView)
        {
            using (var db = new StudentModelEntity())
            {
                var space = db.Teacher.AsQueryable();
                if (!string.IsNullOrWhiteSpace(StuView.CourseName))
                {
                    space = space.Where(s => s.TeaName.Contains(StuView.CourseName.Trim()));
                }
                if (StuView.CourseType > 0)
                {
                    space = space.Where(s => s.ClassId == StuView.CourseType);
                }
                var list = space.Select(s => new TeacherViewModel()
                {
                    TeaId = s.TeaId,
                    ClassId = s.ClassId,
                    ClassName = s.Class.ClassName,
                    TeaName = s.TeaName,
                    TeaAge = s.TeaAge,
                    TeaSex = s.TeaSex
                }).OrderBy(s => s.TeaId).Skip((StuView.Page - 1) * StuView.Limit).Take(StuView.Limit).ToList();
                var totle = space.Count();
                return Json(new { code = 0, msg = "加载成功", count = totle, data = list });
            }
        }

        [HttpGet]
        public ActionResult TeacherAdd()
        {
            return View();
        }

        [HttpPost]
        public ActionResult TeacherAdd(TeacherAddViewModel TeaAddView)
        {
            using (var db = new StudentModelEntity())
            {
                var model = new Teacher()
                {
                    ClassId = TeaAddView.ClassId,
                    TeaName = TeaAddView.TeaName,
                    TeaAge = TeaAddView.TeaAge,
                    TeaSex = TeaAddView.TeaSex
                };
                db.Teacher.Add(model);
                if (db.SaveChanges() > 0)
                {
                    return Json(new Operate(result.succes, "添加成功"));
                }
                else
                {
                    return Json(new Operate(result.erroe, "添加失败"));
                }
            }
        }


        [HttpGet]
        public ActionResult TeacherEdit(int id)
        {
            using (var db = new StudentModelEntity())
            {
                var model = db.Teacher.FirstOrDefault(s => s.TeaId == id);
                if (model != null)
                {
                    var viewModel = new TeacherEditViewModel()
                    {
                        TeaId = model.TeaId,
                        ClassId = Convert.ToInt32(model.ClassId),
                        TeaAge = model.TeaAge,
                        TeaSex = model.TeaSex,
                        TeaName = model.TeaName
                    };
                    ViewData["List"] = Bind();
                    return View(viewModel);
                }
                else
                {
                    return HttpNotFound();
                }
            }
        }

        [HttpPost]
        public ActionResult TeacherEdit(TeacherEditViewModel viewModel)
        {
            using (var db = new StudentModelEntity())
            {
                var model = db.Teacher.FirstOrDefault(s => s.TeaId == viewModel.TeaId);
                if (model != null)
                {
                    model.TeaSex = viewModel.TeaSex;
                    model.ClassId = viewModel.ClassId;
                    model.TeaAge = viewModel.TeaAge;
                    model.TeaName = viewModel.TeaName;
                    if (db.SaveChanges() > 0)
                    {
                        return Json(new Operate(result.succes, "编辑成功"));
                    }
                    else
                    {
                        return Json(new Operate(result.erroe, "编辑失败"));
                    }
                }
                else
                {
                    return Json(new Operate(result.erroe, "更新数据不存在"));
                }
            }
        }

        [HttpPost]
        public ActionResult TeacherDel(int DelId)
        {
            using (var db = new StudentModelEntity())
            {
                var model = db.Teacher.FirstOrDefault(s => s.TeaId == DelId);
                if (model != null)
                {
                    db.Teacher.Remove(model);
                    if (db.SaveChanges() > 0)
                    {
                        return Json(new Operate(result.succes, "删除成功!"));
                    }
                    else
                    {
                        return Json(new Operate(result.erroe, "删除失败!"));
                    }
                }
                else
                {
                    return Json(new Operate(result.erroe, "数据不存在!"));
                }
            }
        }

        private List<SelectListItem> Bind()
        {
            using (var db = new StudentModelEntity())
            {
                var model = db.Class.Where(s => true).Select(s=>new SelectListItem() {
                    Text=s.ClassName,
                    Value=s.Id.ToString()
                }).ToList();
                return model;
            }
        }
    }
}